Sample notebook for multiple linear regression (MLR) for Abalone data

アワビデータの線形重回帰分析の手順例

Data: https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/
Abalone Data (modified: some data are replaced by N/A):

Sex / nominal / -- / M, F, and I (infant)
Length / continuous / mm / Longest shell measurement
Diameter / continuous / mm / perpendicular to length
Height / continuous / mm / with meat in shell
Whole weight / continuous / grams / whole abalone
Shucked weight / continuous / grams / weight of meat
Viscera weight / continuous / grams / gut weight (after bleeding)
Shell weight / continuous / grams / after being dried
Rings / integer / -- / +1.5 gives the age in years

Import libraries

Import my libraries

Parameters

Step 1. Collect possible explanatory variables

目的変数に影響を与えていそうな要因は、可能な限り網羅的に説明変数に取り入れる。

Check & read CSV file, replace column labels if needed, etc.

encoding='shift-jis' may be needed.
CSVファイルをチェックしてから読み込む。必要に応じて列ラベルを変更。
CSVファイルの漢字コードがShift-JISの場合は encoding='shift-jis' が必要。  

Check numerical / category variables if needed

数値列・カテゴリー列の様子をみる

See rows including missing data

欠損値を含む行を表示してみる

Delete rows including missing data, or fill missing data

欠損値を含む行を削除する (または欠損値を埋める)

Separate explanatory variables and objective variable

説明変数と目的変数を分ける

Apply get_dummies()

ダミー変数化

Step 2. Scatter plot and correlation coefficients between all combination of explanatory variables

変数間の総当たり散布図を描画。相関係数も算出しておく

all by all Pearson correlation coefficients;

総当たりのPearson相関係数

Pickup explanatory variable pairs with large absolute value of correlation coefficient;

相関係数の絶対値が大きい説明変数ペアの出力

Method 1. Straight forward method

Method 2. Rather tricky method ...

all by all scatter plots of explanatory variables;

変数間の総当たり散布図

if you want to use seaborn instead of matplotlib

seabornを使うなら

Heatmap

Heatmapを描いてもよい

Step 3. MLR calculation using all variables

全説明変数を用いて、標準化なしで線形重回帰分析

Step 4. Check R2 and Adjusted R2 to see whether MLR is appropriate for this data

決定係数や自由度調整済み決定係数をみて、そもそも線形モデルの当てはめが妥当かどうかを判断

Rather good ...

Step 5. Stat. test for MLR equation

重回帰式の検定 (求めた重回帰式は目的変数を説明している?)

Very small p-value, so this MLR equation is considered to be significant.

Step 6. Standardization of variables

Compare coefficients for explanatory variables
全説明変数と目的変数を標準化して線形重回帰分析
標準化偏回帰係数を比較  

Step 7. Feature selection

変数選択

by forward selection method based on AIC

変数増加法による変数選択

Step 8. Check of multicolinearity (VIF checkup)

マルチコのチェック

Format of results of statsmodels: https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.RegressionResults.html

How to eliminate multicolinearity is case by case.
Here we simply delete three explanatory variables with high VIF.

For all explantory variables, VIF < 10, so we can go forward.

Step 9. Estimate the magnitude of the influence of each explanatory variable on the objective variable

最終的に得られた標準化偏回帰係数から、各説明変数の目的変数に対する影響の大きさがわかる

The order of strengths of influences on objective variables:
d (positive) > h (positive) > w_gut (negative) > sex_I (negative)
(これが、目的変数ringに対する各説明変数(d, h, w_gut, sex_I)の影響の大きさ順)

Step 10. Stat. test for MLR equation

重回帰式の検定 (求めた重回帰式は目的変数を説明している?)

Very small p-value, so this MLR equation is considered to be significant.

Step 11. MLR calculation using selected explanatory variables

選択された説明変数を用いて、標準化なしで線形重回帰分析

Step 12. Check R2 and Adjusted R2

決定係数や自由度調整済み決定係数をみて、線形モデルの当てはまりの良さをチェック

The fit of "the best model" is not good ...

Step 13. partial regression coefficients

最終的に得られた偏回帰係数から、「各説明変数が1増えたときの目的変数の増分」がわかる。

Coefficients of MLR;
Increment of objective variable when corresponding variable is increased by 1
and other variables are not changed

Obtained best model:

ring $\sim$ 2.770 + 20.075 h + 13.822 d + (-5.129) w_gut + (-1.088) sex_I

Step 14. Do prediction using best model

NOTICE: add 1 at the head of each data for constant.
(You can use sm.add_constant() to add 1) 得られたベストモデルを用いて、予測を行う。
注: 予測のための各Xデータの先頭には定数項用の1を付加すること。
(sm.add_constant()を用いて付加してもよい)